/*----------------------------------------------------------------------*/
/* NAME : mainslp.c                                                     */
/* Auth : dikim at gnewsoft                                            */
/* Date : 2011.08.                                                      */
/*----------------------------------------------------------------------*/

#include "inc.h"

#define VERSION_INFO        "1.0"
#define CAP_CFG_FILE        "cap/capsim.ini"

int     		_nProcName = SIM_PROC;
char     		*_nCfgFileName = CAP_CFG_FILE;
char     		*_nShmKey = CAPSIM_SHM_KEY;
int				_nUdpTimer;

//int             *_pnLogLevel;
int             _nFlush = OFF;
int             _nSysId;
int             _nSysType;
int             _nFileSize;
int             _nSeqId;

int             _nTcpWCap= -1;
//int             nSd= -1;

PTBL_HD         _nTblHd;
PTBL2_HD        _nTblHdMore;
char            _szInitFile[128];

_CAP_TABLE		*_pstShmCAP;

int             _nAlmCode[64];
_ST_TABLE    	_stSt;

int				_nUdpSt		= 	-1;

int				_nUdpGen	=	-1;

int		_nFgFileSize;
int		_nTot;
int		_nLastSize;
char	*_pszFgData;

pthread_mutex_t _mEstLock = PTHREAD_MUTEX_INITIALIZER;
int             _nCapId     = 0;
char            _szGenUdp[16];

int			_gnFromSn = 1;
int			_gnToSn = 1;


int     _nTotCnt=0;
int     _nSuccCnt=0;
int     _nFailCnt=0;
int     _nProgressCnt=0;
int		_gnPerf_f = OFF;


int main(int argc, char *argv[])
{
    int         i, nOpt;
    int         nTag_f=ON, nTag_m=OFF, nTag_c=OFF;
    int         nFirstF;
   	pthread_t   p_thread[MAX_THREAD];
   	int   		arg[MAX_THREAD];
	_THREAD_INFO	*pThr;
    //char        szBuf[128];
    //pthread_t   tid1, tid2, tid3;
    char        szFileName[128];
    FILE		*fp = NULL;
    char		buf[128];

    sprintf(buf, "/opt/%s/var/lock/.lock.fsim", get_core());
    makefulldir(buf, 0777);

    sprintf(buf, "/opt/%s/var/lock/.lock.fsim", get_core());
    int n = lock_process(buf);
    if (n) {
        fprintf(stderr, "FSIM Already running. To continue stop it !! (%s)\n", buf);
        return 0;
    }
    fprintf(stderr, "FSIM Starting....\n");
    
    if( goEnv() < 0 ) { printf("goEnv Fail\n"); exit(0); }
        
    strcpy(_szInitFile, GetCfgPath(NULL, _nCfgFileName));

	printf("InitFile(%s)\n", _szInitFile);
    
#if	0
    while( (nOpt=getopt(argc, argv, "hfmci:")) != EOF ) {
        switch( nOpt ) {
            case 'i' : strcpy(szFileName, optarg); break;
            case 'r' : _nCapId = atoi(optarg); break;
            case 'f' : nTag_f = ON; break;
            case 'c' : nTag_c = ON; break;
            case 'm' : nTag_m = ON; break;
            case 'h' :
                printf("\n");
                printf("--------------  [ HELP ]  ------------------------\n");
                printf("  %s [-hfmci:]\n", argv[0]);
                printf("    -h : Help\n");
                printf("    -f : Force Clear All of Ctl Memory\n");
                printf("    -c : Force Clear All of Mng Memory\n");
                printf("    -m : RFM flag\n");
                printf("    -i [init_file] : Process Init File\n");
                printf("--------------------------------------------------\n");
                exit(0);
            default :   break;
        }
    }
#endif

	if ( argc == 3 ) {
		_gnFromSn = atoi(argv[1]);
		_gnToSn = atoi(argv[2]);
	}else{
		printf("Usage : psim 1 2\n");
		exit(0);
	}
	_nSeqId = _gnFromSn;

	if ( _gnToSn - _gnFromSn > 2 ) {
		printf("Perfomance mode\n");
		_gnPerf_f = ON;
	}

	printf("START SN (%.6d ~ %.6d)\n", _gnFromSn, _gnToSn);

    for(i=0; i<20; i++) signal(i,Sigh);
    signal(SIGHUP,SIG_IGN);
    signal(SIGPIPE,SIG_IGN);
    signal(SIGCHLD,SIG_IGN);
    signal(SIGUSR1,SighUsr);
    signal(SIGUSR2,SighUsr2);

    memset(_szGenUdp, 0x00, sizeof(_szGenUdp));
    sprintf(_szGenUdp, "sim_%d", _nCapId);
    _nProcName = _nProcName + _nCapId;

    if( (_nTblHd = InitProcTblWith(_nProcName, nTag_m)) == NULL ) {
        printf("InitProcTblWith Fail !!\n");
        exit(1);
    }

    _nSysId = GetSysId( _nTblHd );
    _nSysType = GetSysType( _nTblHd );

    if( (nFirstF=Open_Memory(_nTblHd, nTag_m)) < 0) {
        printf("Memory Open Fail !!\n");
        exit(0);
    }

    if( nFirstF == ON ) nTag_c = nTag_f = ON;

    if( Init_Memory(nTag_f, nTag_c, _szInitFile) < 0 ) {
        printf("Memory Init Fail !!\n");
        exit(1);
    }
    Init_AlmCode();

    SetVersion(_nTblHd, _nProcName, VERSION_INFO);

    _pnLogLevel =GetLogLvlArea(_nTblHd, _nProcName);
    if( nTag_f ) *_pnLogLevel = SIM_SYSP.nLogLevel;

#if	0
    if( OpenLog(SIM_SYSP.szLogFile, _pnLogLevel, &_nFlush) < 0 ) {
        printf("Open Log Fail\n");
        exit(1);
    }
#else
	if( OpenLogEx(SIM_SYSP.szLogFile, _pnLogLevel, &_nFlush, 200000000) < 0 ) {
        printf("Open Log Fail\n");
        exit(1);
    }
#endif
    if(LOG_C & *_pnLogLevel)
        Log(LOG_C, (char *)"nTag_c: [%d]\n", nTag_c );

    if( (_nTblHdMore = InitProcTblMore()) == NULL ) {
        printf("InitProcTblMore Fail !!\n");
        exit(1);
    }
	Print_Cfg();

	for(i=_gnFromSn; i<SIM_SYSP.nMaxCap; i++) {

        Log(LOG_D, "[%4d] INIT Calling Thread\n", i);
        pThr = &THR(i);

		memset(pThr, 0x00, sizeof(_THREAD_INFO));

pthread_mutex_lock(&pThr->lock);
		pThr->nState = THREAD_INIT;
pthread_mutex_unlock(&pThr->lock);
		pThr->nSd = -1;

        if( (pThr->pQ = itq_create()) == NULL ) {
            Log(LOG_C, "[%4d] INIT Error: Creating ITQ\n", i);
            exit(0);
        }
        Log(LOG_I, "[%4d] Created Queue\n", i);
        if( pthread_mutex_init( &pThr->lock, NULL ) != 0 )
        {
            Log( LOG_C, "Mutex Init Err[%d:%s]\n", errno, strerror(errno) );
            exit(1);
        }
    }

	for(i=0; i<SIM_SYSP.nMaxCap; i++) {

        pThr = &THR(i);
		arg[i] = i;
        if(work_thread_create(i, pThr, ThrWork, (void *)&arg[i], 1)<0) { //  inInfo
        // if((perr=pthread_create(&p_thread[i], NULL, ThrWork, (void *)i))<0) { 
            Log( LOG_C, "Thread Create Err[%d:%s]\n", errno, strerror(errno) );
            exit(1);
		}
		//Sleep_m(10);
	}
    // Print_CfgInfo();
    // Init Alarm Library
#if	0
    if( InitAlmMngLib(ON, 60, 10, _nTblHd) < 0 ) {
        Log(LOG_C, (char *)"InitAlmMngLib Fail\n");
        exit(0);
    }
#endif

    pthread_t   tid;
    if (pthread_create(&tid, NULL, ThrResult, (void *)0) != 0) {
        Log(LOG_C, (char *)"[MAIN] pthread_create(ThrResult) failed\n");
        exit(0);
    }

    Run();
	return 0;
}

void * ThrResult(void *arg)
{
    while(1)
    {
        printf("+ TOTAL(%4d) PROGRESS(%d) SUCC(%4d)\n",
            _nTotCnt, _nTotCnt-_nSuccCnt, _nSuccCnt);

        Log(LOG_C, "+ TOTAL(%4d) PROGRESS(%d) SUCC(%4d)\n",
            _nTotCnt, _nTotCnt-_nSuccCnt, _nSuccCnt);

        sleep(1);
    }
    return (void*)0;
}

void Init_AlmCode()
{
    //_nAlmCode[CAP_ALM_THRION]  =   1954;
	//_nAlmCode[CAP_ALM_ASP_SESSION] 	=	1956; 
}

void Run()
{
	int		nTry = 0, nApply = 0, j, nTot;
    char    inbuf[1024];
    FILE	*infp = stdin;
	int		nSaveThr=0;
	int		nNonStop = 0;

    int             nRet, i;
    struct          timeval timeout;
    fd_set          fdIn;

	memset(inbuf, 0x00, sizeof(inbuf));

	printf("SIMULATOR READY\n");

	nTry = nApply = 0 ;

#if	0
	if ( fgets(inbuf, 1000, infp) != NULL ) {
		nApply 	= 0;
		nTry 	= atoi(inbuf);

		if ( nTry == 999 )	nNonStop = 1;
	}
	printf("START TRY(%4d)\n", nTry);
#else
	nTry = 1;
#endif

	nTot = _gnToSn - _gnFromSn;

	printf("nTot : %d (%d ~ %d)\n", nTot, _gnFromSn, _gnToSn);

	while (1) {
		for(i=0; i<nTry; i++) {
			// Log(LOG_C, "APPLY(%4d)\n", nApply);

			while ( GetIdleThread() < 0 )	{
				printf("Suspending...\n");
				Sleep_m(1000);
			}
			nApply++;
			_nTotCnt++;
		}
		// printf("Apply(%d) nTot(%d)\n", nApply, nTot);
		if ( nTot <= nApply ) break;
		Sleep_m(500);
	}
	
	printf("end\n");

	while(1) sleep(1);
}

int GetIdleThread()
{
    int i;

    for(i=RSRC_CTRL.nLastHuntThr; i<SIM_SYSP.nMaxCap; i++) {

//        Log(LOG_I, "[%4d] GetIdleThread nSd(%d)\n", i, THR(i).nSd);

pthread_mutex_lock(&THR(i).lock);
        if ( THR(i).nState == THREAD_INIT )  {
            RSRC_CTRL.nLastHuntThr++;
            if ( RSRC_CTRL.nLastHuntThr > SIM_SYSP.nMaxCap )
                RSRC_CTRL.nLastHuntThr = 0;

            THR(i).nState = THREAD_IDLE;
pthread_mutex_unlock(&THR(i).lock);
            return i;
        }
pthread_mutex_unlock(&THR(i).lock);
    }
    for(i=0; i<RSRC_CTRL.nLastHuntThr; i++) {

 //       Log(LOG_I, "[%4d] GetIdleThread nSd(%d)\n", i, THR(i).nSd);
pthread_mutex_lock(&THR(i).lock);
        if ( THR(i).nState == THREAD_INIT )  {
            RSRC_CTRL.nLastHuntThr++;
            if ( RSRC_CTRL.nLastHuntThr > SIM_SYSP.nMaxCap )
                RSRC_CTRL.nLastHuntThr = 0;

            THR(i).nState = THREAD_IDLE;
pthread_mutex_unlock(&THR(i).lock);
            return i;
        }
pthread_mutex_unlock(&THR(i).lock);
    }

    Log(LOG_C, (char *)"%s%sCannot found idle thread MaxThr(%d) LastHuntThr(%d)%s\n",
        E_BOLD, T_RED, SIM_SYSP.nMaxCap, RSRC_CTRL.nLastHuntThr, E_RELEASE);
    return -1;
}

int Open_Memory(PTBL_HD _nTblHd, int nTag_m)
{
    int     nKey, nShmId, nFirstF;
    int     nSemId;


    if( (nSemId = SemGet(GetSemPath(NULL, _nShmKey),1)) < 0 ) {
        printf("ccf : Open Sem(%s) create fail !!: %s\n", GetSemPath(NULL,_nShmKey), strerror(errno));
        return -1;
    }
    printf("Open SEM : SemId(%d): Path(%s)\n", nSemId, GetSemPath(NULL,_nShmKey));

    if( (nShmId=SharedCreate( GetShmPath(NULL,_nShmKey), sizeof(_CAP_TABLE), &nKey, &nFirstF) ) < 0 ) {
        printf("ccf : ccf table shared memroy create fail [%s] key[%#x]\n", strerror(errno), nKey);
        SemUnlock(nSemId, 0);
        return -1;
    }

    _pstShmCAP = (_CAP_TABLE *) shmat(nShmId, NULL, 0);
    printf("Open SHM : file(%s), id(%d), key(%x), first(%d), size(%ld)\n",
        GetShmPath(NULL,CAPSIM_SHM_KEY), nShmId, nKey, nFirstF, sizeof(_CAP_TABLE));

    _pstShmCAP->stTblInfo.nShmID = nShmId;
    _pstShmCAP->stTblInfo.nKey = nKey;

    _pstShmCAP->stTblInfo.nSize = sizeof(_CAP_TABLE);
    _pstShmCAP->stTblInfo.tCreat = time(NULL);

    return  SUCC;
}


int Init_Memory(int nTag_f, int nTag_c, char *_szInitFile)
{
    int         i, nWCnt;
    char        szBuf[256];
    char        *pszWords[64];
    int 		perr=0;
    //struct hostent *    pstHp;
    //struct servent *    pstSp;
    //unsigned long       nInaddr;
    //int 		nAsrIdx, nTtsIdx, nDspIdx;

    if( nTag_f ) {
        ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"LOG_LEVEL", (char *)"", szBuf);
		printf("LOG_LEVEL (%s)\n", szBuf);
        if( szBuf[0] ) {
            nWCnt = C_WordsInLine(szBuf, pszWords, 128);
            for(i=0, SIM_SYSP.nLogLevel=0; i<nWCnt; i++) switch( toupper((int)pszWords[i][0]) ) {
                case 'C' : SIM_SYSP.nLogLevel |= LOG_C;  break;
                case 'L' : SIM_SYSP.nLogLevel |= LOG_L;  break;
                case 'R' : SIM_SYSP.nLogLevel |= LOG_R;  break;
                case 'N' : SIM_SYSP.nLogLevel |= LOG_N;  break;
                case 'I' : SIM_SYSP.nLogLevel |= LOG_I;  break;
                case 'D' : SIM_SYSP.nLogLevel |= LOG_D;  break;
                case '1' : SIM_SYSP.nLogLevel |= LOG_LOCAL1; break;
                case '2' : SIM_SYSP.nLogLevel |= LOG_LOCAL2; break;
                case '3' : SIM_SYSP.nLogLevel |= LOG_LOCAL3; break;
                case '4' : SIM_SYSP.nLogLevel |= LOG_LOCAL4; break;
                case '5' : SIM_SYSP.nLogLevel |= LOG_LOCAL5; break;
                case '6' : SIM_SYSP.nLogLevel |= LOG_LOCAL6; break;
                case '7' : SIM_SYSP.nLogLevel |= LOG_LOCAL7; break;
                case '8' : SIM_SYSP.nLogLevel |= LOG_LOCAL8; break;
                case '9' : SIM_SYSP.nLogLevel |= LOG_LOCAL9; break;
                default :   break;
            }
            SIM_SYSP.nLogLevel |= LOG_NODELAY;
        }
		for(i=0; i<MAX_THREAD; i++) {
			memset(&THR(i), 0x00, sizeof(_THREAD_INFO));
		}
		//TODO _pstShmCAP memset 0x00 dikim.
    }
    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"LOG_FILE", (char *)"/log/fota/cap/capsim.log", szBuf);
    sprintf(SIM_SYSP.szLogFile, "%s.%d", szBuf, _nCapId);

    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"CAP_IP", (char *)"127.0.0.1", szBuf);
    sprintf(SIM_SYSP.szCapIp, szBuf);

    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"CAP_PORT", (char *)"9976", szBuf);
    sprintf(SIM_SYSP.szCapPort, szBuf);

    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"HB_INTERVAL", (char *)"10", szBuf);
	SIM_SYSP.nHbInterval = atoi(szBuf);

    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"HB_TIMEOUT", (char *)"5", szBuf);
	SIM_SYSP.nHbTimeOut = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"NOFTHREADS", (char *)"10", szBuf);
	SIM_SYSP.nMaxCap = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"FILEWRITE", (char *)"1", szBuf);
	SIM_SYSP.nFileFlag = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"STARTINTERVAL", (char *)"1000", szBuf);
	SIM_SYSP.nStartIvl = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"PACKETSIZE", (char *)"2036", szBuf);
	SIM_SYSP.nPacketSize = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"NEXTJOB", (char *)"0200", szBuf);
	SIM_SYSP.nNextJob = atoi(szBuf);

	ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"SERVICE_CODE", (char *)"60", szBuf);
	SIM_SYSP.nServiceCode = atoi(szBuf);

/***********************/

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"CARD_ID", (char *)"905239313037", szBuf);
	strcpy(SIM_SYSP.stCmn.cardId, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"CARDKIND_CODE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.cardType, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"MERCHANT_CODE", (char *)"0005092000", szBuf);
	strcpy(SIM_SYSP.stCmn.merchantBranchCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"HANG_CODE", (char *)"11680", szBuf);
	strcpy(SIM_SYSP.stCmn.hangCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"DEVICE_TYPE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.deviceType, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"GLOBAL_SERIALNO", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.globalSerialNo, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"SALECODE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.saleCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"PRICE", (char *)"1000", szBuf);
	strcpy(SIM_SYSP.stCmn.price, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"KIND", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.giftCardKind, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"EXCODE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.exchangeCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"GIFTKIND_CODE", (char *)"14", szBuf);
	strcpy(SIM_SYSP.stCmn.giftKindCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"AMOUNT", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.settlementAmount, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"PASSWD", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.passWord, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"CASHIER", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.cashierId, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"BARCODE", (char *)"0", szBuf);
	strcpy(MSG.barCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"BILLNO", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.billNo, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"ORG_DATE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.originalApprovalDate, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"ORG_APPROVALNO", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.originalApprovalNo, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"ORG_CRDAPPROVALNO", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.originalcrdApprovalNo, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"ORG_AMOUNT", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.originalApprovalAmount, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"ORG_SALECODE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.originalSaleCode, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"PRODUCT_COUNT", (char *)"1", szBuf);
	strcpy(SIM_SYSP.stCmn.productCount, szBuf);

	ReadConfFile(_szInitFile, (char *)"COMMON" , (char *)"BARCODE", (char *)"0", szBuf);
	strcpy(SIM_SYSP.stCmn.barcode, szBuf);

/***********************/

	int		nCount = atoi(SIM_SYSP.stCmn.productCount);

	for(i=0; i<nCount; i++)
	{
		char	szBuf2[128];

		sprintf(szBuf2, "PRODUCT_%.2d", i);

		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"ID", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productId, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"BARCODE", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productBarcode, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"NUMBER", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productNumber, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"PROVIDER_CODE", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productProviderCode, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"COST", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productCost, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"COPRICE", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productCoPrice, szBuf);
		
		ReadConfFile(_szInitFile, (char *)szBuf2, (char *)"MAPRICE", (char *)"1", szBuf);
		strcpy(SIM_SYSP.stCmn.stPlu[i].productMaPrice, szBuf);
		
	}
/***********************/

    return SUCC;
}


void
Print_Cfg()
{
	int		i;
	char	szBuf[512];

    Log(LOG_I, (char *)"LOGFILE    : %s\n", SIM_SYSP.szLogFile);
    Log(LOG_I, (char *)"CAP IP     : %s\n", SIM_SYSP.szCapIp);
    Log(LOG_I, (char *)"CAP PORT   : %s\n", SIM_SYSP.szCapPort);
    Log(LOG_I, (char *)"NOFTHREADS : %d\n", SIM_SYSP.nMaxCap);
    Log(LOG_I, (char *)"PACKETSIZE : %d\n", SIM_SYSP.nPacketSize);
    Log(LOG_I, (char *)"NEXTJOB : %d\n", SIM_SYSP.nNextJob);
    Log(LOG_I, (char *)"SERVICECODE : %d\n", SIM_SYSP.nServiceCode);

	Log(LOG_I, (char *)"CARD_ID : %s\n", SIM_SYSP.stCmn.cardId);
	Log(LOG_I, (char *)"CARDKIND_CODE : %s\n", SIM_SYSP.stCmn.cardType);
	Log(LOG_I, (char *)"MERCHANT_CODE : %s\n", SIM_SYSP.stCmn.merchantBranchCode);
	Log(LOG_I, (char *)"HANG_CODE : %s\n", SIM_SYSP.stCmn.hangCode);
	Log(LOG_I, (char *)"DEVICE_TYPE : %s\n", SIM_SYSP.stCmn.deviceType);
	Log(LOG_I, (char *)"GLOBAL_SERIALNO : %s\n", SIM_SYSP.stCmn.globalSerialNo);
	Log(LOG_I, (char *)"SALECODE : %s\n", SIM_SYSP.stCmn.saleCode);
	Log(LOG_I, (char *)"PRICE : %s\n", SIM_SYSP.stCmn.price);
	Log(LOG_I, (char *)"KIND : %s\n", SIM_SYSP.stCmn.giftCardKind);
	Log(LOG_I, (char *)"EXCODE : %s\n", SIM_SYSP.stCmn.exchangeCode);
	Log(LOG_I, (char *)"GIFTKIND_CODE : %s\n", SIM_SYSP.stCmn.giftKindCode);
	Log(LOG_I, (char *)"AMOUNT : %s\n", SIM_SYSP.stCmn.settlementAmount);
	Log(LOG_I, (char *)"PASSWD : %s\n", SIM_SYSP.stCmn.passWord);
	Log(LOG_I, (char *)"CASHIER : %s\n", SIM_SYSP.stCmn.cashierId);
	Log(LOG_I, (char *)"BARCODE : %s\n", MSG.barCode);
	Log(LOG_I, (char *)"BILLNO : %s\n", SIM_SYSP.stCmn.billNo);
	Log(LOG_I, (char *)"ORG_DATE : %s\n", SIM_SYSP.stCmn.originalApprovalDate);
	Log(LOG_I, (char *)"ORG_APPROVALNO : %s\n", SIM_SYSP.stCmn.originalApprovalNo);
	Log(LOG_I, (char *)"ORG_CRDAPPROVALNO : %s\n", SIM_SYSP.stCmn.originalcrdApprovalNo);
	Log(LOG_I, (char *)"ORG_AMOUNT : %s\n", SIM_SYSP.stCmn.originalApprovalAmount);
	Log(LOG_I, (char *)"ORG_SALECODE : %s\n", SIM_SYSP.stCmn.originalSaleCode);
	Log(LOG_I, (char *)"PRODUCT_COUNT : %s\n", SIM_SYSP.stCmn.productCount);
	Log(LOG_I, (char *)"BARCODE : %s\n", SIM_SYSP.stCmn.barcode);

/***********************/

	int		nCount = atoi(SIM_SYSP.stCmn.productCount);

	for(i=0; i<nCount; i++)
	{
		sprintf(szBuf, "PRODUCT_%.2d", i);

		Log(LOG_I, (char *)"[%s] ID %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productId);
		Log(LOG_I, (char *)"[%s] BARCODE %s\n", szBuf,  SIM_SYSP.stCmn.stPlu[i].productBarcode);
		Log(LOG_I, (char *)"[%s] NUMBER %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productNumber);
		Log(LOG_I, (char *)"[%s] PROVIDER_CODE %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productProviderCode);
		Log(LOG_I, (char *)"[%s] COST %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productCost);
		Log(LOG_I, (char *)"[%s] COPRICE %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productCoPrice);
		Log(LOG_I, (char *)"[%s] MAPRICE %s\n", szBuf, SIM_SYSP.stCmn.stPlu[i].productMaPrice);
	}
}



void Sigh(int n)
{
    int		i;

    Log(LOG_C, (char *)"Catch The Signal %d\n",n);
    CloseLog();

	for(i=0; i<SIM_SYSP.nMaxCap; i++) {
		if ( THR(i).nSd > 0 ) close(THR(i).nSd);
		THR(i).nSd = -1;
	}

    exit(0);
}

void SighUsr2(int n)
{
    //int nAsrIdx, nTtsIdx;
	int		i;

    Log(LOG_C, (char *)"Catch The usr2 Signal !!\n");

	for(i=0; i<SIM_SYSP.nMaxCap; i++) {
		if ( THR(i).nSd > 0 ) close(THR(i).nSd);
		THR(i).nSd = -1;
	}

    signal(SIGUSR2,SighUsr2);
}


void SighUsr(int n)
{
    int         i, nWCnt;
    char        szBuf[256];
    char        *pszWords[64];
    int         nLogLevel = SIM_SYSP.nLogLevel;

    ReadConfFile(_szInitFile, (char *)"SYSDEF" , (char *)"LOG_LEVEL", "", szBuf);

    if( szBuf[0] ) {
        nWCnt = C_WordsInLine(szBuf, pszWords, 128);
        for(i=0, SIM_SYSP.nLogLevel=0; i<nWCnt; i++) switch( toupper((int)pszWords[i][0]) )
        {
            case 'C' : SIM_SYSP.nLogLevel |= LOG_C;  break;
            case 'L' : SIM_SYSP.nLogLevel |= LOG_L;  break;
            case 'R' : SIM_SYSP.nLogLevel |= LOG_R;  break;
            case 'N' : SIM_SYSP.nLogLevel |= LOG_N;  break;
            case 'I' : SIM_SYSP.nLogLevel |= LOG_I;  break;
            case 'D' : SIM_SYSP.nLogLevel |= LOG_D;  break;
            case '1' : SIM_SYSP.nLogLevel |= LOG_LOCAL1; break;
            case '2' : SIM_SYSP.nLogLevel |= LOG_LOCAL2; break;
            case '3' : SIM_SYSP.nLogLevel |= LOG_LOCAL3; break;
            case '4' : SIM_SYSP.nLogLevel |= LOG_LOCAL4; break;
            case '5' : SIM_SYSP.nLogLevel |= LOG_LOCAL5; break;
            case '6' : SIM_SYSP.nLogLevel |= LOG_LOCAL6; break;
            case '7' : SIM_SYSP.nLogLevel |= LOG_LOCAL7; break;
            case '8' : SIM_SYSP.nLogLevel |= LOG_LOCAL8; break;
            case '9' : SIM_SYSP.nLogLevel |= LOG_LOCAL9; break;
            default :   break;
        }
        SIM_SYSP.nLogLevel |= LOG_NODELAY;
    }

    *_pnLogLevel = SIM_SYSP.nLogLevel;
    Log(LOG_C, (char *)"Chage LogLevel (0x%x -> 0x%x)\n", nLogLevel, SIM_SYSP.nLogLevel);
    signal(SIGUSR1, SighUsr);
}
